//
//  SocketDataHandle.m
//  Karaoke
//
//  Created by Jiang Haofeng on 12-7-29.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "SocketDataHandle.h"
#import "ServerNetworkCenter.h"
#import "ClientNetworkCenter.h"
#import "MTStatusBarOverlay.h"
// #import "GongGuanObject.h"

@implementation SocketDataHandle
static SocketDataHandle *_sharedInstance;
@synthesize delegate;
@synthesize tmpSelectFood;
@synthesize Amount, addInfo;
@synthesize roomState;
@synthesize reportObj;

@synthesize isControlVol, isMute, isOriginalMusic, isPlaying, currentVol;

@synthesize volValue, huaTongVol, hunXiangVol, effMode;

@synthesize tempRoomArray, volMax, micMax, effMax;
@synthesize loginID, loginPwd;

+ (SocketDataHandle *)sharedInstance
{
    if (!_sharedInstance)
    {
        _sharedInstance = [[SocketDataHandle alloc] init];
    }

    return _sharedInstance;
}

- (id)init
{
    self = [super init];

    if (self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDataFromSocket:) name:@"getDataFromCenter" object:nil];
        tmpSelectFood = [[OKSelectFoodObject alloc] init];
    }

    return self;
}

- (void)didReceiveDataFromSocket:(NSNotification *)notification
{
    // NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
    NSString    *tmpCommandCode = [[notification userInfo] objectForKey:@"code"];
    NSString    *tmpCommandDetail = [[notification userInfo] objectForKey:@"detail"];

    if ([tmpCommandCode isEqualToString:@"confirmInputAmount"])
    {
        if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidConfirmInputAmount)])
        {
            [self.delegate socketDataHandleDidConfirmInputAmount];
        }

        return;
    }

    if ([tmpCommandCode isEqualToString:@"sysMsg"])
    {
        if ([tmpCommandDetail isEqualToString:@"clientConnected"])
        {
            CLog(@"have connected to server");
            [[ClientNetworkCenter sharedInstance] sentMsgAskForBillList];

            if ([self.delegate respondsToSelector:@selector(socketDataHandle:clientConnected:)])
            {
                [self.delegate socketDataHandle:self clientConnected:YES];
            }
        }
        else
        if ([tmpCommandDetail isEqualToString:@"serverConnected"])
        {
            CLog(@"have connected to server");

            //            [[ServerNetworkCenter sharedInstance] sentMsgAskForUpdate];
            [MTStatusBarOverlay sharedInstance].lblOffLine.text = @"已连接";
            [MTStatusBarOverlay sharedInstance].offLineImage.image = [UIImage imageNamed:@"Computer On"];

            if ([self.delegate respondsToSelector:@selector(socketDataHandle:serverConnect:)])
            {
                [self.delegate socketDataHandle:self serverConnect:YES];
            }
        }
        else
        if ([tmpCommandDetail isEqualToString:@"serverDidDisconnect"])
        {
            [MTStatusBarOverlay sharedInstance].lblOffLine.text = @"断开连接";
            [MTStatusBarOverlay sharedInstance].offLineImage.image = [UIImage imageNamed:@"Computer Off"];

            if ([self.delegate respondsToSelector:@selector(socketDataHandle:serverConnect:)])
            {
                [self.delegate socketDataHandle:self serverConnect:NO];
            }
            else
            {
                [[ServerNetworkCenter sharedInstance] initConnect];
            }
        }
        else
        if ([tmpCommandDetail isEqualToString:@"clientConnected"])
        {}
        else
        if ([tmpCommandDetail isEqualToString:@"billListUpdate"])
        {
            if ([self.delegate respondsToSelector:@selector(socketDataHandleDidUpdateBillList)])
            {
                [self.delegate socketDataHandleDidUpdateBillList];
            }
        }
    }

    if ([tmpCommandCode isEqualToString:@"tcpMsg"])
    {
        CLog(@"update layer received msg: %@", tmpCommandDetail);
        NSArray     *tmpArray = [tmpCommandDetail componentsSeparatedByString:@"\r\n"];
        NSInteger   cmd = [[tmpArray objectAtIndex:1] intValue];
        switch (cmd)
        {
            case 3:
                {
                    CLog(@"the url is: %@", [[NSUserDefaults standardUserDefaults] stringForKey:@"dbVersion"]);
                    NSURL       *tempURL = [NSURL URLWithString:[tmpArray objectAtIndex:3]];
                    NSString    *currentVersion = [tmpArray objectAtIndex:2];

                    if (([[NSUserDefaults standardUserDefaults] stringForKey:@"dbVersion"] == nil) || ![[[NSUserDefaults standardUserDefaults] stringForKey:@"dbVersion"] isEqualToString:currentVersion])
                    {
                        if ([self.delegate respondsToSelector:@selector(UpdateDB:dbVersion:)])
                        {
                            [self.delegate UpdateDB:tempURL dbVersion:currentVersion];
                        }
                    }
                    else
                    {
                        [self.delegate DbExist];
                    }
                }

                break;

            case rtBound:

                if ([self.delegate respondsToSelector:@selector(socketDataHandle:boundClient:)])
                {
                    [self.delegate socketDataHandle:self boundClient:tmpArray];
                }

                break;

            case rtFoodType:
                {
                    NSMutableDictionary *catagorySet = [[NSMutableDictionary alloc] init];
                    [catagorySet removeAllObjects];

                    for (int i = 5; i < [tmpArray count] - 1; i += 2)
                    {
                        [catagorySet setObject:[tmpArray objectAtIndex:i + 1] forKey:[tmpArray objectAtIndex:i]];
                    }

                    if (catagorySet.count > 0)
                    {
                        if ([self.delegate respondsToSelector:@selector(socketDataHandleDidCategory:)])
                        {
                            [self.delegate socketDataHandleDidCategory:catagorySet];
                        }
                    }
                }
                break;

            case rtFoodList:
                {
                    NSMutableArray *tmpFoodArray = [[NSMutableArray alloc] init];

                    for (int i = 5; i < [tmpArray count] - 1; i += 11)
                    {
                        OKFoodObject *food = [[OKFoodObject alloc] init];
                        food.No = [tmpArray objectAtIndex:i];
                        food.NameChs = [tmpArray objectAtIndex:i + 1];
                        food.NameEng = [tmpArray objectAtIndex:i + 2];
                        food.Price = [tmpArray objectAtIndex:i + 3];
                        food.Monad = [tmpArray objectAtIndex:i + 4];
                        food.Stand = [tmpArray objectAtIndex:i + 5];
                        food.LimitFree = [tmpArray objectAtIndex:i + 6];
                        food.IfPack = [tmpArray objectAtIndex:i + 7];
                        food.AddInfoNo1 = [tmpArray objectAtIndex:i + 8];
                        food.AddInfoNo2 = [tmpArray objectAtIndex:i + 9];
                        food.IfFlowerItem = [tmpArray objectAtIndex:i + 10];
                        [tmpFoodArray addObject:food];
                    }

                    if ([self.delegate respondsToSelector:@selector(socketDataHandleDidFoodList:)])
                    {
                        [self.delegate socketDataHandleDidFoodList:tmpFoodArray];
                    }
                }
                break;

            case rtRoomStateAndInfo:
                {
                    roomState = [[RoomStateAndInfo alloc] init];

                    if (tmpArray.count > 4)
                    {
                        
                        roomState.Flag = [tmpArray objectAtIndex:3];
                        roomState.RoomType = [tmpArray objectAtIndex:4];
                        roomState.MasterID = [tmpArray objectAtIndex:5];
                        roomState.PriceID = [tmpArray objectAtIndex:6];
                        roomState.DisRate = [tmpArray objectAtIndex:7];
                        roomState.DisClerk = [tmpArray objectAtIndex:8];
                        roomState.ReserveFlag = [tmpArray objectAtIndex:9];
                        roomState.ReserveClerkNo = [tmpArray objectAtIndex:10];
                        roomState.ReserveClerk = [tmpArray objectAtIndex:11];
                        roomState.ReserveClerkGroup = [tmpArray objectAtIndex:12];
                        roomState.GongGuanLeaderNo = [tmpArray objectAtIndex:13];
                        roomState.GongGuanLeaderName = [tmpArray objectAtIndex:14];
                        roomState.GongGuanCount = [tmpArray objectAtIndex:15];
                        roomState.GuestName = [tmpArray objectAtIndex:16];
                        roomState.ManCount = [tmpArray objectAtIndex:17];
                        roomState.RewardClerkNo = [tmpArray objectAtIndex:18];
                        roomState.RewardClerk = [tmpArray objectAtIndex:19];
                        roomState.RewardZhiWu = [tmpArray objectAtIndex:20];
                        roomState.TransInRoomNo = [tmpArray objectAtIndex:21];
                        roomState.TransOutRoomNo = [tmpArray objectAtIndex:22];
                        roomState.LowLimitMoney = [tmpArray objectAtIndex:23];
                        roomState.XiaoFeiJinE = [tmpArray objectAtIndex:24];
                        roomState.DiXiaoNeiZheHouXiaoFeiJinE = [tmpArray objectAtIndex:25];
                        roomState.DiXiaoWaiJinE = [tmpArray objectAtIndex:26];
                        roomState.IfVip = [tmpArray objectAtIndex:27];
                        roomState.VipID = [tmpArray objectAtIndex:28];
                        roomState.RoomMoneyMode = [tmpArray objectAtIndex:29];
                        roomState.SettleMode = [tmpArray objectAtIndex:30];
                        roomState.DJ1_XingMing = [tmpArray objectAtIndex:31];
                        roomState.DJ2_XingMing = [tmpArray objectAtIndex:32];
                        roomState.DJ3_XingMing = [tmpArray objectAtIndex:33];
                        roomState.Server1_XingMing = [tmpArray objectAtIndex:34];
                        [DataCenter sharedInstance].printId = [tmpArray objectAtIndex:35];
                        roomState.MasterLockFlag = [tmpArray objectAtIndex:37];
                        roomState.PreSettleFlag = [tmpArray objectAtIndex:38];
                        roomState.State = [tmpArray objectAtIndex:2];
                    }

                    if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidRoomStateAndInfo:)])
                    {
                        [self.delegate socketDataHandleDidRoomStateAndInfo:roomState];
                    }
                }
                break;

            case rtAddInfoOfFood:

                if (tmpArray.count > 3)
                {
                    NSMutableArray *infoArray = [[NSMutableArray alloc] init];

                    for (int i = 3; i < tmpArray.count - 1; i++)
                    {
                        [infoArray addObject:[tmpArray objectAtIndex:i]];
                    }

                    if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidAddInfoOfFood:)])
                    {
                        [self.delegate socketDataHandleDidAddInfoOfFood:infoArray];
                    }
                }

                break;

            case rtSelectFood:
                {
                    self.tmpSelectFood.ErrorCode = [tmpArray objectAtIndex:2];
                    self.tmpSelectFood.HasMultFreeItems = [tmpArray objectAtIndex:3];
                    self.tmpSelectFood.MultFreeCount = [tmpArray objectAtIndex:4];
                    self.tmpSelectFood.MultFreeItemsSetID = [tmpArray objectAtIndex:5];
                    self.tmpSelectFood.GroupID = [tmpArray objectAtIndex:6];
                    self.tmpSelectFood.DataCount = [tmpArray objectAtIndex:7];

                    if ([tmpSelectFood.ErrorCode isEqualToString:@"1"])
                    {
                        UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                                    message             :@"菜品已缺货！"
                                                                    delegate            :self
                                                                    cancelButtonTitle   :@"确定"
                                                                    otherButtonTitles   :nil, nil];
                        [alert show];

                        return;
                    }

                    if (![tmpSelectFood.ErrorCode isEqualToString:@"0"])
                    {
                        return;
                    }

                    NSMutableArray *tmpFolios = [[NSMutableArray alloc] init];

                    for (int j = 0, i = 8; j < tmpSelectFood.DataCount.intValue; j++, i += 27)
                    {
                        OKFolioObject *tmpFolio = [[OKFolioObject alloc] init];
                        tmpFolio.FoodNo = [tmpArray objectAtIndex:i + 0];
                        tmpFolio.FoodName = [tmpArray objectAtIndex:i + 1];
                        tmpFolio.Price = [tmpArray objectAtIndex:i + 2];
                        tmpFolio.Flag = [tmpArray objectAtIndex:i + 3];
                        tmpFolio.Quantity = [[tmpArray objectAtIndex:i + 4] intValue];
                        tmpFolio.CanDiscount = [tmpArray objectAtIndex:i + 5];
                        tmpFolio.Monad = [tmpArray objectAtIndex:i + 6];
                        tmpFolio.IfLowLimitPrice = [tmpArray objectAtIndex:i + 7];
                        tmpFolio.PrinterNo = [tmpArray objectAtIndex:i + 8];
                        tmpFolio.OverTimes = [tmpArray objectAtIndex:i + 8];
                        tmpFolio.IfPack = [tmpArray objectAtIndex:i + 10];
                        tmpFolio.IfPackChild = [tmpArray objectAtIndex:i + 11];
                        tmpFolio.AutoFreed = [tmpArray objectAtIndex:i + 12];
                        tmpFolio.AutoFreeCount = [tmpArray objectAtIndex:i + 13];
                        tmpFolio.ParentFoodNo = [tmpArray objectAtIndex:i + 14];
                        tmpFolio.CanReward = [tmpArray objectAtIndex:i + 15];
                        tmpFolio.AutoFreeSetIDOfMutilToOne = [tmpArray objectAtIndex:i + 16];
                        tmpFolio.LimitDisRate = [tmpArray objectAtIndex:i + 17];
                        tmpFolio.SendMobileMsgWhenFree = [tmpArray objectAtIndex:i + 18];
                        tmpFolio.Note = [tmpArray objectAtIndex:i + 19];
                        tmpFolio.VipPrice = tmpArray[i + 20];
                        tmpFolio.UseVipSpecialPrice = tmpArray[i + 21];
                        tmpFolio.retain1 = tmpArray[i + 22];
                        tmpFolio.retain2 = tmpArray[i + 23];
                        tmpFolio.retain3 = tmpArray[i + 24];
                        tmpFolio.retain4 = tmpArray[i + 25];
                        tmpFolio.retain5 = tmpArray[i + 26];
                        tmpFolio.Adds1Name = self.addInfo.length > 0 ? self.addInfo : @"";
                        tmpFolio.Adds2Name = @"";
                        tmpFolio.GroupID = tmpSelectFood.GroupID;
                        tmpFolio.FlowerGirlsNo = @"";
                        [tmpFolios addObject:tmpFolio];
                    }

                    if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidSelectFood:)])
                    {
                        [self.delegate socketDataHandleDidSelectFood:tmpFolios];
                    }
                }
                break;

            case rtPackChild:
                {
                    if (tmpArray.count > 1)
                    {
                        NSInteger packCount = [[tmpArray objectAtIndex:2] integerValue];

                        if (packCount == 0)
                        {
                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidPackChild:packCount:packChildArray:)])
                            {
                                [self.delegate socketDataHandleDidPackChild:nil packCount:packCount packChildArray:nil];
                            }

                            return;
                        }

                        NSMutableArray *packChildArray = [NSMutableArray array];

                        for (int i = 4, j = 0; j < packCount; j++, i += 3)
                        {
                            OKPackChild *pack = [[OKPackChild alloc] init];
                            pack.SetID = [tmpArray objectAtIndex:i];
                            pack.SetItemName = [tmpArray objectAtIndex:i + 1];
                            pack.SetSumCount = [NSString stringWithFormat:@"%d", ((NSString *)[tmpArray objectAtIndex:i + 2]).intValue * self.Amount];
                            [packChildArray addObject:pack];
                        }

                        NSMutableDictionary *dict = [NSMutableDictionary dictionary];

                        for (int i = packCount * 3 + 4; i < tmpArray.count - 1; i += 6)
                        {
                            OKPackChildFood *packFood = [[OKPackChildFood alloc] init];
                            packFood.SetID = [tmpArray objectAtIndex:i];
                            packFood.FoodNo = [tmpArray objectAtIndex:i + 1];
                            packFood.FoodName = [tmpArray objectAtIndex:i + 2];
                            packFood.Monad = [tmpArray objectAtIndex:i + 3];
                            packFood.PrintType = [tmpArray objectAtIndex:i + 4];
                            packFood.OverTimes = [tmpArray objectAtIndex:i + 5];

                            NSMutableArray *tmp = [dict objectForKey:packFood.SetID];

                            if (!tmp)
                            {
                                tmp = [NSMutableArray array];
                                [dict setObject:tmp forKey:packFood.SetID];
                            }

                            [tmp addObject:packFood];
                        }

                        if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidPackChild:packCount:packChildArray:)])
                        {
                            [self.delegate socketDataHandleDidPackChild:dict packCount:packCount packChildArray:packChildArray];
                        }
                    }
                }
                break;

            case rtAutoFreeFood:
                {
                    if (tmpArray.count > 1)
                    {
                        NSInteger packCount = [[tmpArray objectAtIndex:2] integerValue];

                        if (packCount == 0)
                        {
                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidAutoFreeFood:packCount:)])
                            {
                                //                            [self.delegate stoketDataHandle:self didAutoFreeFood:nil packCount:packCount];
                                [self.delegate socketDataHandleDidAutoFreeFood:nil packCount:packCount];
                            }

                            break;
                        }

                        NSMutableArray *packChildArray = [[NSMutableArray alloc] init];

                        for (int i = 4, j = 0; j < packCount; j++, i += 4)
                        {
                            OKPackChild *pack = [[OKPackChild alloc] init];
                            pack.SetID = [tmpArray objectAtIndex:i];            // 自动赠送项目分类设置的ID对应TAutoFreeSetItems.SetID

                            pack.SetItemName = [tmpArray objectAtIndex:i + 1];  // 自动赠送项目分类设置的名称

                            pack.SetNote = [tmpArray objectAtIndex:i + 2];      // 自动赠送的备注

                            pack.SetSumCount = [tmpArray objectAtIndex:i + 3];  // 该类项目设置的可选的累积赠送数量

                            [packChildArray addObject:pack];

                            pack.CanSelectCount = pack.SetSumCount.intValue * tmpSelectFood.MultFreeCount.intValue; // 该类项目可选的累计赠送数量
                        }

                        NSMutableDictionary *dict = [NSMutableDictionary dictionary];

                        for (int i = packCount * 4 + 4; i < tmpArray.count - 1; i += 7)
                        {
                            OKPackChildFood *packFood = [[OKPackChildFood alloc] init];
                            packFood.SetID = [tmpArray objectAtIndex:i];
                            packFood.FoodNo = [tmpArray objectAtIndex:i + 1];
                            packFood.FoodName = [tmpArray objectAtIndex:i + 2];
                            packFood.Price = [tmpArray objectAtIndex:i + 3];
                            packFood.Monad = [tmpArray objectAtIndex:i + 4];
                            packFood.PrintType = [tmpArray objectAtIndex:i + 5];
                            packFood.OverTimes = [tmpArray objectAtIndex:i + 6];
                            NSMutableArray *tmp = [dict objectForKey:packFood.SetID];

                            if (!tmp)
                            {
                                tmp = [[NSMutableArray alloc] init];
                                [dict setObject:tmp forKey:packFood.SetID];
                            }

                            [tmp addObject:packFood];
                        }

                        if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidAutoFreeFood:packCount:)])
                        {
                            //                        [self.delegate stoketDataHandle:self didAutoFreeFood:dict packCount:packCount];
                            [self.delegate socketDataHandleDidAutoFreeFood:dict packCount:packCount];
                        }
                    }
                }

                break;

            case rtConfirmInputFood:
                {
                    if (tmpArray.count > 3)
                    {
                        if (![[tmpArray objectAtIndex:2] isEqualToString:@"0"])
                        {
                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidConfirmInputFood:)])
                            {
                                [self.delegate socketDataHandleDidConfirmInputFood:[tmpArray objectAtIndex:3]];
                            }
                        }
                        else
                        {
                            [tmpSelectFood.foodSet removeAllObjects];

                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidConfirmInputFood:)])
                            {
                                [self.delegate socketDataHandleDidConfirmInputFood:@""];
                            }
                        }

                        break;
                    }
                }

            case pcMuteState:
                self.isMute = [[tmpArray objectAtIndex:2] isEqualToString:@"1"];

                if ([self.delegate respondsToSelector:@selector(socketDataHandleDidControlChenged)])
                {
                    [self.delegate socketDataHandleDidControlChenged];
                }

                break;

            case pcYC:
                self.isOriginalMusic = YES;

                if ([self.delegate respondsToSelector:@selector(socketDataHandleDidControlChenged)])
                {
                    [self.delegate socketDataHandleDidControlChenged];
                }

                break;

            case pcBC:
                self.isOriginalMusic = NO;

                if ([self.delegate respondsToSelector:@selector(socketDataHandleDidControlChenged)])
                {
                    [self.delegate socketDataHandleDidControlChenged];
                }

                break;

            case pcVolume:
                self.currentVol = [[tmpArray objectAtIndex:2] intValue];

                if ([self.delegate respondsToSelector:@selector(socketDataHandleDidControlChenged)])
                {
                    [self.delegate socketDataHandleDidControlChenged];
                }

                break;

            case pcPauseState:
                self.isPlaying = [[tmpArray objectAtIndex:2] isEqualToString:@"1"];

                if ([self.delegate respondsToSelector:@selector(socketDataHandleDidControlChenged)])
                {
                    [self.delegate socketDataHandleDidControlChenged];
                }

                break;

            case rtGetRoomReport:
                {
                    NSString *str = [tmpArray objectAtIndex:2];
                    NSLog(@"%@", str);

                    if (str.intValue == 0)
                    {
                        reportObj = [[ReportObject alloc] init];
                        reportObj.reportID = ((NSString *)[tmpArray objectAtIndex:3]).intValue;
                        reportObj.YinXiangZhiLiang = ((NSString *)[tmpArray objectAtIndex:4]).intValue;
                        reportObj.YinXiangZaYin = ((NSString *)[tmpArray objectAtIndex:5]).intValue;
                        reportObj.HuaTongHunXiang = ((NSString *)[tmpArray objectAtIndex:6]).intValue;
                        reportObj.HuaTongXiaoJiao = ((NSString *)[tmpArray objectAtIndex:7]).intValue;
                        reportObj.DianShiTuXiang = ((NSString *)[tmpArray objectAtIndex:8]).intValue;
                        reportObj.GongFangYunZuo = ((NSString *)[tmpArray objectAtIndex:9]).intValue;
                        reportObj.GeKuGengXin = ((NSString *)[tmpArray objectAtIndex:10]).intValue;
                        reportObj.YinXiangShiPinBeiZhu = [tmpArray objectAtIndex:11];
                        reportObj.IPADXingHao = ((NSString *)[tmpArray objectAtIndex:12]).intValue;
                        reportObj.ShouJiXingHao = ((NSString *)[tmpArray objectAtIndex:13]).intValue;
                        reportObj.WangLuoXiaoGuoBeiZhu = [tmpArray objectAtIndex:14];
                        reportObj.KongTiaoXiaoGuo = ((NSString *)[tmpArray objectAtIndex:15]).intValue;
                        reportObj.KongTiaoZaYin = ((NSString *)[tmpArray objectAtIndex:16]).intValue;
                        reportObj.PaiFengXinFeng = ((NSString *)[tmpArray objectAtIndex:17]).intValue;
                        reportObj.KongQiZhiLiang = ((NSString *)[tmpArray objectAtIndex:18]).intValue;
                        reportObj.FangNeiShiDu = ((NSString *)[tmpArray objectAtIndex:19]).intValue;
                        reportObj.FangNeiYiWei = ((NSString *)[tmpArray objectAtIndex:20]).intValue;
                        reportObj.KongTiaoXiaoGuoBeiZhu = [tmpArray objectAtIndex:21];
                        reportObj.ChuPinSuDu = ((NSString *)[tmpArray objectAtIndex:22]).intValue;
                        reportObj.ShuiGuoZhiLiang = ((NSString *)[tmpArray objectAtIndex:23]).intValue;
                        reportObj.ShuiGuoPinZhong = ((NSString *)[tmpArray objectAtIndex:24]).intValue;
                        reportObj.ShuiGuoXingZhuang = ((NSString *)[tmpArray objectAtIndex:25]).intValue;
                        reportObj.XiaoChiZhiLiang = ((NSString *)[tmpArray objectAtIndex:26]).intValue;
                        reportObj.XiaoChiXingZhuang = ((NSString *)[tmpArray objectAtIndex:27]).intValue;
                        reportObj.XiaoChiKouWei = ((NSString *)[tmpArray objectAtIndex:28]).intValue;
                        reportObj.XiaoChiPinZhong = ((NSString *)[tmpArray objectAtIndex:29]).intValue;
                        reportObj.ChuPinBuBeiZhu = [tmpArray objectAtIndex:30];
                        reportObj.XiShouJianWeiSheng = ((NSString *)[tmpArray objectAtIndex:31]).intValue;
                        reportObj.FangJianPeiHe = ((NSString *)[tmpArray objectAtIndex:32]).intValue;
                        reportObj.WuPinJiaoJie = ((NSString *)[tmpArray objectAtIndex:33]).intValue;
                        reportObj.WuPinGenJin = ((NSString *)[tmpArray objectAtIndex:34]).intValue;
                        reportObj.LouMianBuBeiZhu = [tmpArray objectAtIndex:35];
                        reportObj.ShiFangLiMao = ((NSString *)[tmpArray objectAtIndex:36]).intValue;
                        reportObj.ShiFangLieDui = ((NSString *)[tmpArray objectAtIndex:37]).intValue;
                        reportObj.LunFangSuDu = ((NSString *)[tmpArray objectAtIndex:38]).intValue;
                        reportObj.KeHuWeiHu = ((NSString *)[tmpArray objectAtIndex:39]).intValue;
                        reportObj.FangNeiFuWu = ((NSString *)[tmpArray objectAtIndex:40]).intValue;
                        reportObj.FangNeiFenWei = ((NSString *)[tmpArray objectAtIndex:41]).intValue;
                        reportObj.FangNeiPeiHe = ((NSString *)[tmpArray objectAtIndex:42]).intValue;
                        reportObj.SongKeQingKuang = ((NSString *)[tmpArray objectAtIndex:43]).intValue;
                        reportObj.YeWuBuBeiZhu = [tmpArray objectAtIndex:44];
                        reportObj.DaiFangHuiFangCiShu = ((NSString *)[tmpArray objectAtIndex:45]).intValue;
                        reportObj.DaiFangJingJiuCiShu = ((NSString *)[tmpArray objectAtIndex:46]).intValue;
                        reportObj.DaiFangHuoYueQiFenCiShu = ((NSString *)[tmpArray objectAtIndex:47]).intValue;
                        reportObj.QiTaBeiZhu = [tmpArray objectAtIndex:48];
                    }

                    if ([self.delegate respondsToSelector:@selector(socketDataHandleDidRoomReport:)])
                    {
                        [self.delegate socketDataHandleDidRoomReport:self.reportObj];
                    }
                }
                break;

            case rtSaveRoomReport:
                {
                    if ([[tmpArray objectAtIndex:2] isEqualToString:@"0"])
                    {
                        if ([self.delegate respondsToSelector:@selector(socketDataHandleDidSaveRoomReport)])
                        {
                            [self.delegate socketDataHandleDidSaveRoomReport];
                        }
                    }
                }
                break;

            case rtHallBound:
                {
                    if (tmpArray.count > 2)
                    {
                        if ([[tmpArray objectAtIndex:2] isEqualToString:@"0"])
                        {
                            [DataCenter sharedInstance].clerkNo = [tmpArray objectAtIndex:4];
                            [DataCenter sharedInstance].ClerkName = [tmpArray objectAtIndex:5];
                            [MTStatusBarOverlay sharedInstance].lblClerk.text = [NSString stringWithFormat:@"操作人：%@", [tmpArray objectAtIndex:5]];

                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidHallBound:errorMsg:)])
                            {
                                [self.delegate socketDataHandleDidHallBound:YES errorMsg:@""];
                            }

                            return;
                        }
                        else
                        {
                            if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidHallBound:errorMsg:)])
                            {
                                [self.delegate socketDataHandleDidHallBound:NO errorMsg:[tmpArray objectAtIndex:3]];
                            }
                        }
                    }
                }
                break;

            case rtDianShiPinDao:
                {
                    NSLog(@"%d", tmpArray.count);
                    NSMutableArray *tvRelayArray = [[NSMutableArray alloc] init];

                    for (int i = 2; i < tmpArray.count - 1; i += 3)
                    {
                        TVRelayObject *tvRelay = [[TVRelayObject alloc] init];
                        tvRelay.orderNum = [tmpArray objectAtIndex:i];
                        tvRelay.name = [tmpArray objectAtIndex:i + 1];
                        tvRelay.number = [tmpArray objectAtIndex:i + 2];
                        [tvRelayArray addObject:tvRelay];
                    }

                    if (tvRelayArray.count > 0)
                    {
                        if ([self.delegate respondsToSelector:@selector(socketDataHandleDidDianShiPinDao:)])
                        {
                            [self.delegate socketDataHandleDidDianShiPinDao:tvRelayArray];
                        }
                    }
                    else
                    {
                        tvRelayArray = nil;
                    }
                }
                break;

            case rtShowGongGuan:

                //                if (tmpArray.count > 2)
                //                {
                //                    NSMutableArray *gongGuanArray = [[NSMutableArray alloc] init];
                //
                //                    for (int i = 2; i < tmpArray.count - 1; i += 5)
                //                    {
                //                        GongGuanObject *obj = [[GongGuanObject alloc] init];
                //                        obj.bianHao = [tmpArray objectAtIndex:i];
                //                        obj.xingMing = [tmpArray objectAtIndex:i + 1];
                //                        obj.yiMing = [tmpArray objectAtIndex:i + 2];
                //                        obj.jiGuan = [tmpArray objectAtIndex:i + 3];
                //                        obj.zhuangTai = [tmpArray objectAtIndex:i + 4];
                //                        [gongGuanArray addObject:obj];
                //                    }
                //
                //                    if ([self.delegate respondsToSelector:@selector(socketDataHandleDidGongGuanList:)])
                //                    {
                //                        [self.delegate socketDataHandleDidGongGuanList:gongGuanArray];
                //                    }
                //                }
                //
                break;

            case pcTiaoGuangState:
                {
                    /*
                     *   当调光的某一项参数发生变化后，包房主机的向连接该主机的客户端(IPAD)广播该项新的状态。
                     *   状态分类不同，后面跟的参数也不同，以下列出各种状态分类以及参数的含义
                     *   命令=0:主开关状态
                     *   参数1:0:关 1:开
                     *   命令=1:排风状态
                     *   参数1:0:关 1:开
                     *   命令=2:空调一状态
                     *   参数1:0:关 1:一档；2:二档;3:三档
                     *   命令=3:空调二状态
                     *   参数1:0:关 1:一档；2:二档;3:三档
                     *   命令=4:空调三状态
                     *   参数1:0:关 1:一档；2:二档;3:三档
                     *
                     *   命令=7:当前灯光模式
                     *   参数1:1-12分别对应12种模式（目前能显示在界面上的是1-6种模式）。
                     *   命令=11-26:分别对应1—16路灯光的当前亮度
                     *   参数1:各路灯光的亮度值。
                     *   命令=30:各路灯光的名称
                     *   参数1:各路灯光的名称，中间用“;”分割。
                     *   包房主机收到客户端命令并执行命令后，还应对连接到该主机的客户端(Ipad)广播pcTiaoGuangState命令。
                     */
                    NSString    *str = [tmpArray objectAtIndex:2];
                    NSString    *state = [tmpArray objectAtIndex:3];

                    if (![state isEqualToString:@"C"] && ![state isEqualToString:@"B"])
                    {
                        switch (str.intValue)
                        {
                            case 0:
                                {
                                    [TiaoGuangStateObject sharedInstance].KaiGuanState = state;
                                }
                                break;

                            case 1:
                                {
                                    [TiaoGuangStateObject sharedInstance].PaiFengState = state;
                                }
                                break;

                            case 2:
                                {
                                    [TiaoGuangStateObject sharedInstance].KongTiaoState1 = state;
                                }
                                break;

                            case 3:
                                {
                                    [TiaoGuangStateObject sharedInstance].KongTiaoState2 = state;
                                }
                                break;

                            case 4:
                                {
                                    [TiaoGuangStateObject sharedInstance].KongTiaoState3 = state;
                                }
                                break;

                            case 7:
                                {
                                    [TiaoGuangStateObject sharedInstance].CurrentMode = state;
                                    [TiaoGuangStateObject sharedInstance].currentModeValue = state.integerValue;
                                }
                                break;

                            case 30:
                                {
                                    [TiaoGuangStateObject sharedInstance].names = [state componentsSeparatedByString:@";"];
                                }
                                break;

                            case 11:
                                [TiaoGuangStateObject sharedInstance].line1Value = state;
                                break;

                            case 12:
                                [TiaoGuangStateObject sharedInstance].line2Value = state;
                                break;

                            case 13:
                                [TiaoGuangStateObject sharedInstance].line3Value = state;
                                break;

                            case 14:
                                [TiaoGuangStateObject sharedInstance].line4Value = state;
                                break;

                            case 15:
                                [TiaoGuangStateObject sharedInstance].line5Value = state;
                                break;

                            case 16:
                                [TiaoGuangStateObject sharedInstance].line6Value = state;
                                break;

                            case 17:
                                [TiaoGuangStateObject sharedInstance].line7Value = state;
                                break;

                            case 18:
                                [TiaoGuangStateObject sharedInstance].line8Value = state;
                                break;

                            case 19:
                                [TiaoGuangStateObject sharedInstance].line9Value = state;
                                break;

                            case 20:
                                [TiaoGuangStateObject sharedInstance].line10Value = state;
                                break;

                            case 21:
                                [TiaoGuangStateObject sharedInstance].line11Value = state;
                                break;

                            case 22:
                                [TiaoGuangStateObject sharedInstance].line12Value = state;
                                break;

                            default:
                                {}
                                 break;
                        }

                        if ([self.delegate respondsToSelector:@selector(socketDataHandleDidDengGuang)])
                        {
                            [self.delegate socketDataHandleDidDengGuang];
                        }
                    }
                }
                break;

            case pcTiaoYinState:
                {
                    /*
                     *   当调光的某一项参数发生变化后，包房主机的向连接该主机的客户端(IPAD)广播该项新的状态。
                     *   状态分类不同，后面跟的参数也不同，以下列出各种状态分类以及参数的含义
                     *   命令=0：调节主音量
                     *   参数：主音量大小
                     *   命令=1：话筒音量
                     *   参数：当前话筒音量大小
                     *   命令=2：混响深度
                     *   参数：当前混响深度大小
                     *   命令=4：当前效果器预设的模式
                     *   参数：1-6预设的模式
                     */
                    NSString    *s = [tmpArray objectAtIndex:2];
                    NSString    *value = [tmpArray objectAtIndex:3];
                    switch (s.intValue)
                    {
                        case 0:
                            volValue = value;
                            break;

                        case 1:
                            huaTongVol = value;
                            break;

                        case 2:
                            hunXiangVol = value;
                            break;

                        case 4:
                            effMode = value;
                            break;

                        case 5:
                            self.volMax = value.floatValue;
                            self.micMax = ((NSString *)[tmpArray objectAtIndex:4]).floatValue;
                            self.effMax = ((NSString *)[tmpArray objectAtIndex:5]).floatValue;
                            break;

                        default:

                            break;
                    }

                    if ([self.delegate respondsToSelector:@selector(socketDataHandleDidTiaoYin)])
                    {
                        [self.delegate socketDataHandleDidTiaoYin];
                    }
                }
                break;

            case rtRoomStates:
                {
                    if (tmpArray.count > 5)
                    {
                        RoomStatesObject *obj = [self getRoomStates:tmpArray];
                        
                        if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidRoomStates:)])
                        {
                            [self.delegate socketDataHandleDidRoomStates:obj];
                        }
                    }

                }
                break;

            case rtRoomRegistClerk:
                {
                    if ([[tmpArray objectAtIndex:2] isEqualToString:@"0"])
                    {
                        [DataCenter sharedInstance].clerkNo = [tmpArray objectAtIndex:4];
                        [DataCenter sharedInstance].clerkName = [tmpArray objectAtIndex:5];

                        if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(socketDataHandleDidLoginSucceed)])
                        {
                            [self.delegate socketDataHandleDidLoginSucceed];
                        }
                    }
                    else
                    {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[tmpArray objectAtIndex:3] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                        [alert show];
                    }
                }

                break;

            case rtSysDate:
                {
                    _sysDate = [tmpArray objectAtIndex:3];
                    [MTStatusBarOverlay sharedInstance].lblWifi.text = [NSString stringWithFormat:@"系统日期：%@", [tmpArray objectAtIndex:3]];
                }
                break;

            default:
                break;
        }
    }
}
- (RoomStatesObject *)getRoomStates:(NSArray *)tmpArray
{
    RoomStatesObject *roomObj = [[RoomStatesObject alloc] init];
    
    roomObj.NullCount = ((NSString *)tmpArray[5]).integerValue;
    roomObj.LeadCount = ((NSString *)tmpArray[6]).integerValue;
    roomObj.ReserverCount = ((NSString *)tmpArray[7]).integerValue;
    roomObj.InCount = ((NSString *)tmpArray[8]).integerValue;
    roomObj.SettleCount = ((NSString *)tmpArray[9]).integerValue;
    roomObj.RetainCount = ((NSString *)tmpArray[10]).integerValue;
    roomObj.MergerCount = ((NSString *)tmpArray[11]).integerValue;
    roomObj.BadCount = ((NSString *)tmpArray[12]).integerValue;
    roomObj.QNullCount = ((NSString *)tmpArray[13]).integerValue;
    roomObj.QReserverCount = ((NSString *)tmpArray[14]).integerValue;
    roomObj.QInCount = ((NSString *)tmpArray[15]).integerValue;
    roomObj.QSettleCount = ((NSString *)tmpArray[16]).integerValue;
    
    for (int i = 17; i < tmpArray.count - 1; i += 38)
    {
        RoomState *state = [[RoomState alloc] init];
        state.RoomNo             = [tmpArray objectAtIndex:i];
        state.RoomState          = [tmpArray objectAtIndex:i + 1];
        state.RoomFlag           = [tmpArray objectAtIndex:i + 2];
        state.Room_IfBad         = [tmpArray objectAtIndex:i + 3];
        state.ReserveClerk       = [tmpArray objectAtIndex:i + 4];
        state.GongGuanLeaderName = [tmpArray objectAtIndex:i + 5];
        state.GuestName          = [tmpArray objectAtIndex:i + 6];
        state.GuestCount         = [tmpArray objectAtIndex:i + 7];
        state.VipFlag            = [tmpArray objectAtIndex:i + 8];
        state.GongGuanCount      = [tmpArray objectAtIndex:i + 9];
        state.ReserveDateTime    = [tmpArray objectAtIndex:i + 10];
        state.ArriveDateTime     = [tmpArray objectAtIndex:i + 11];
        state.CheckInDateTime    = [tmpArray objectAtIndex:i + 12];
        state.LowLimitMoney      = [tmpArray objectAtIndex:i + 13];
        state.FoodMoney          = [tmpArray objectAtIndex:i + 14];
        state.OverLowLimitMoney  = [tmpArray objectAtIndex:i + 15];
        state.MasterID           = [tmpArray objectAtIndex:i + 16];
        state.CheckOutDateTime   = [tmpArray objectAtIndex:i + 17];
        state.FreeMoney          = [tmpArray objectAtIndex:i + 18];
        state.DisRate            = [tmpArray objectAtIndex:i + 19];
        state.DisMoney           = [tmpArray objectAtIndex:i + 20];
        state.RoomMoney          = [tmpArray objectAtIndex:i + 21];
        state.DinnerMoney        = [tmpArray objectAtIndex:i + 22];
        state.FanFangRen         = tmpArray[i + 23];
        state.FanFangRenFenLei   = tmpArray[i + 24];
        state.MT_PreSettleFlag   = tmpArray[i + 25];
        state.retain4            = tmpArray[i + 26];
        state.retain5            = tmpArray[i + 27];
        state.retain6            = tmpArray[i + 28];
        state.retain7            = tmpArray[i + 29];
        state.retain8            = tmpArray[i + 30];
        state.retain9            = tmpArray[i + 31];
        state.retain10           = tmpArray[i + 32];
        state.retain11           = tmpArray[i + 33];
        state.retain12           = tmpArray[i + 34];
        state.retain13           = tmpArray[i + 35];
        state.retain14           = tmpArray[i + 36];
        state.retain15           = tmpArray[i + 37];
        [roomObj.RoomStates addObject:state];
    }
    
    return roomObj;
}

@end